package com.softeem.service.Impl;

import com.alibaba.druid.support.spring.stat.SpringStatUtils;
import com.softeem.entity.User;
import com.softeem.entity.UserExample;
import com.softeem.entity.WorkOrder;
import com.softeem.mapper.UserMapper;
import com.softeem.mapper.WorkOrderMapper;
import com.softeem.model.LayuiPagingResult;
import com.softeem.model.UserParamModel;
import com.softeem.service.UserService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private WorkOrderMapper OrderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean finishOrder(Integer oId) {
        try {
            WorkOrder workOrder = OrderMapper.selectByPrimaryKey(oId);
            workOrder.setIsFinish(1);
            return OrderMapper.updateByPrimaryKeySelective(workOrder) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean deleteOrder(Integer oId) {
        try {
            return OrderMapper.deleteByPrimaryKey(oId) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean isExist(String param) {
        UserExample example = new UserExample();
        example.createCriteria().andNickNameEqualTo(param);
        if(userMapper.selectByExample(example).size() == 0){
            example.clear();
            example.createCriteria().andPhoneEqualTo(param);
            System.out.println(111);
            if(userMapper.selectByExample(example).size() == 0){
                return false;
            }else {
                return true;
            }
        }else {
            return true;
        }
    }

    @Override
    public boolean addUser(User user) {
        try {
            return userMapper.insertSelective(user) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Integer login(User user, String isKeep, HttpServletRequest req, HttpServletResponse resp) {
        UserExample example = new UserExample();
        example.createCriteria().andPhoneEqualTo(user.getLoginName()).andPasswordEqualTo(user.getPassword());
        List<User> users = userMapper.selectByExample(example);
        User userRes = new User();
        if(users.size() == 0){
            userRes = null;
        }else if(users.size() == 1) {
            userRes = users.get(0);
        }
        if(userRes == null){
            return 0;
        }else{
            Integer state = userRes.getState();
            if(state == null){
                return 2;
            } else if(state == 1){
                Cookie loginNamecookie = new Cookie("loginName", user.getLoginName());
                Cookie password = new Cookie("password",user.getPassword() );
                Cookie isUser = new Cookie("isUser", "isUser");

                if("1".equals(isKeep)){
                    loginNamecookie.setMaxAge(60*60*24*30);
                    password.setMaxAge(60*60*24*30);
                    isUser.setMaxAge(60*60*24*30);
                    System.out.println("keep");
                }else {
                    loginNamecookie.setMaxAge(0);
                    password.setMaxAge(0);
                    isUser.setMaxAge(0);
                    System.out.println("nokeep");
                }

                loginNamecookie.setPath(req.getContextPath());
                password.setPath(req.getContextPath());
                isUser.setPath(req.getContextPath());

                resp.addCookie(loginNamecookie);
                resp.addCookie(password);
                resp.addCookie(isUser);
                Cookie[] cookies = req.getCookies();
                for (Cookie cookie : cookies) {
                    System.out.println(cookie.getName()+","+cookie.getValue());
                }

                req.getSession().setAttribute("userInfo",userRes);
                return 1;
            }else if(state == 0){
                return 3;
            }else {
                return 0;
            }
        }
    }

    @Override
    public LayuiPagingResult<User> getUsersDynamical(UserParamModel paramModel) {
        System.out.println(paramModel);
        LayuiPagingResult<User> result = new LayuiPagingResult<>();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();

        if(paramModel.getId() != null){
            criteria.andIdEqualTo(paramModel.getId());
        }
        if(paramModel.getPassword() != null && !paramModel.getPassword().equals("")){
            criteria.andPasswordLike("%"+paramModel.getPassword()+"%");
        }
        if(paramModel.getNickName() != null && !paramModel.getNickName().equals("")){
            criteria.andNickNameLike("%"+paramModel.getNickName()+"%");
        }
        if(paramModel.getSex() != null && !paramModel.getSex().equals("") && !paramModel.getSex().equals("null")){
            criteria.andSexEqualTo(paramModel.getSex());
        }
        if(paramModel.getPhone() !=null && !paramModel.getPhone().equals("")){
            criteria.andPhoneLike("%"+paramModel.getPhone()+"%");
        }
        if(paramModel.getIsCheck()!=null && paramModel.getIsCheck().equals("true")){
            criteria.andStateIsNull();
        }else if(paramModel.getIsCheck()!=null &&(paramModel.getIsCheck().equals("1") || paramModel.getIsCheck().equals("0"))){
            criteria.andStateEqualTo(Integer.parseInt(paramModel.getIsCheck()));
        }

        if (paramModel.getPage() !=null && paramModel.getLimit() != null){
            paramModel.setPage((paramModel.getPage()-1)*paramModel.getLimit());
        }

        Integer index = paramModel.getPage();
        Integer limit = paramModel.getLimit();

        long count =  userMapper.countByExample(example);
        System.out.println(count);
        List<User> users = new ArrayList<>();

        System.out.println(index);
        System.out.println(limit);
        users = userMapper.selectByExampleLimit(example, index, limit);

        result.setCode(0);
        result.setMsg("");
        result.setCount(count);
        result.setData(users);
        return result;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateUser(User user) {
        try {
          return  userMapper.updateByPrimaryKeySelective(user) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean reCheckUser(Integer id) {
        try {
            return userMapper.reCheckByPrimaryKey(id) > 0;
        }catch (Exception e){
            return false;
        }

    }

    @Override
    public boolean deleteUserById(Integer id) {
        try {
            return userMapper.deleteByPrimaryKey(id) > 0;
        }catch (Exception e){
            return false;
        }
    }


}
